/**
 * 选路
 */
function OutComeModule(ui, engine) {
	this.base = Module;
	this.base(ui, engine);
}
OutComeModule.prototype = new Module();

OutComeModule.prototype.alert = function() {

};

OutComeModule.prototype.openSelector = function(data) {
	this.outComesData = data;
	var outComeModule = this;

	var radioOption = function(val, name, checked) {
		return $('<div class="radio"><label><input type="radio" name="outCome" value="' + val + '" ' + checked + '>' + name + '</label></div>');
	};

	var wrap = $("<div></div>");

	for (var i = 0; i < data.outComes.length; i++) {
		// TODO 这里要考虑审批模块的数据，若审批为同意（或不同意）要对退回路径的选项进行过滤
		var checked = "";
		if (data.outComes[i].checked) {
			checked = "checked";
			outComeModule.checkedOutCome = data.outComes[i];
		}
		var outCome = data.outComes[i];
		var val = outCome.outCome;

		var option = radioOption(val, val, checked);
		option.find("input").attr("index", i);
		option.find("input").click(function() {
			outComeModule.checkedOutCome = outComeModule.outComesData.outComes[$(this).attr("index")];
		});
		wrap.append(option);
	}

	dialog.modal1({
	title : "选择流程路径",
	sourceType : "local",// local 表示内容来源本地, remote 表示内容从网络请求获取
	htmlContent : wrap,// 若sourceType=local时，htmlContent不能为空
	btnFunction : function() {
		outComeModule.submit();
	}
	});
};

OutComeModule.prototype.canBack = function() {
	var outComeModule=this;

	var confirmhandle=function(){
		outComeModule.goBack();
	};

	var todoId = this.engine.params.todoId;

	$.ajax({
	url : ctxPath + '/jbpmService/engine/getPreTask',
	type : 'post',
	dataType : 'text',
	data : 'todoId=' + todoId,
	ajaxStart:function(){},
	success : function(data) {
		var obj = Util.strToObj(data);
		if (obj != null && obj.jumpAble) {
			var message = '您确认退回到【' + obj.destActivity + ':';
			for (var i = 0; i < obj.destUsers.length; i++) {
				message += obj.destUsers[i].name + ',';
			}
			message = message.substring(0, message.length - 1);
			message += '】环节？';
			dialog.confirm(null, confirmhandle, null, message, null);
		} else {
			dialog.alert("不能退回！");
		}
	}
	});
};

OutComeModule.prototype.goBack = function() {
	var action = ctxPath + "/jbpmService/engine/goBackWork";
	this.back(action);
};

OutComeModule.prototype.goOtherBack = function() {
	var action = ctxPath + "/jbpmService/engine/goOtherBackWork";
	this.back(action);
};

OutComeModule.prototype.restart = function() {
	var action = ctxPath + "/jbpmService/engine/restartWork";
	this.back(action);
};

OutComeModule.prototype.back = function(action) {
	var engine=this.engine;
	var todoId = engine.params.todoId;
	$.ajax({
	url : action,
	type : "post",
	dataType : "text",
	data : "todoId=" + todoId,
	success : function(data) {
		var obj = Util.strToObj(data);
		console.log(obj);
		if (obj != null) {
			var info = '已退回到【' + obj.destActivity + ':';
			for (var i = 0; i < obj.destUsers.length; i++) {
				info += obj.destUsers[i].name + ',';
			}
			info = info.substring(0, info.length - 1);
			info += '】环节！';
			dialog.alert(info);
			engine.complete();
		} else {
			dialog.alert("提交异常！");
		}
	}
	});
};

OutComeModule.prototype.forward = function() {
	var action = ctxPath + "/jbpmService/engine/submitOutCome";
	var outCome = this.checkedOutCome.outCome;
	if(this.checkedOutCome.defaulted)
		outCome="";
	var todoId = this.engine.params.todoId;
	var actorModule=this.engine.actorModule;
	$.ajax({
	url : action,
	type : 'post',
	dataType : 'text',
	data : "todoId=" + todoId + "&outCome=" + outCome,
	success : function(data) {
		var obj = Util.strToObj(data);
		if(obj){
			if (obj.type == 'end') {
				actorModule.engine.complete();
				dialog.alert(obj.message);
			} else if (obj.type == 'actor') {
				actorModule.openSelector(obj);
			} else if (obj.type == 'error') {
				dialog.alert(obj.message);
			}
		}else{
			dialog.alert("提交异常！");
		}
	}
	});
};

OutComeModule.prototype.submit = function() {
	console.log(this.checkedOutCome);
	var outCome = this.checkedOutCome;

	if (outCome.backable) {
		this.canBack();
	} else if (outCome.restartable) {
		this.restart();
	} else if (outCome.otherbackable) {
		this.goOtherBack();
	} else {
		this.forward();
	}
};